package chances.epg.ui.taglib.model;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;

import chances.epg.commons.utils.PathUtils;

@SuppressWarnings("serial")
public class EpgPageContext implements java.io.Serializable {
	public final static String NAME = "EPG_PAGE_CONTEXT";
	public final static String DEFALUT_GROUP = "default";

	private String resourcePath;

	private String focusId;

	private Rect viewport;

	private PageContext pageContext;

	private Object userSession;

	private String keyHandler;

	private String playHandler;

	private String backUrl;
	
	private String siteCode;

	private int elementId;

	private Map<String, List<LinkModel>> linkGroups = new LinkedHashMap<String, List<LinkModel>>();

	private List<Widget> widgets = new ArrayList<Widget>();
	private Object accessContext;

	private StringBuilder script = new StringBuilder();

	private Object history = null;

	public String getContextPath() {
		return this.pageContext.getServletContext().getContextPath();
	}

	public String getRequestPath() {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		return (String) request.getAttribute("javax.servlet.forward.request_uri");
	}

	public String getTemplatePath() {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		return PathUtils.getPath(request.getServletPath());
	}

	public EpgPageContext(PageContext pageContext) {
		this.pageContext = pageContext;
		this.viewport = new Rect(0, 0, 1280, 720);
	}

	public void addWidget(Widget widget) {
		widgets.add(widget);
	}

	public void setFocusId(String id) {
		this.focusId = id;

	}

	public int nextId() {
		return elementId++;
	}

	public void addLink(LinkModel model) {
		String aGroup = model.getGroup();
		if (Strings.isNullOrEmpty(aGroup)) {
			aGroup = DEFALUT_GROUP;
		}
		List<LinkModel> links = linkGroups.get(aGroup);
		if (links == null) {
			links = new ArrayList<LinkModel>();
			linkGroups.put(aGroup, links);
		}
		links.add(model);
	}

	public List<LinkModel> getDefaultLinks() {
		return linkGroups.get(DEFALUT_GROUP);
	}

	public List<Widget> getWidgets() {
		return this.widgets;
	}

	public void setResourcePath(String resourcePath) {
		this.resourcePath = resourcePath;
	}

	public Rect getViewport() {
		return viewport;
	}

	public void setViewport(Rect viewport) {
		this.viewport = viewport;
	}

	public String getResourcePath() {
		return resourcePath;
	}

	public String getFocusId() {
		return focusId;
	}

	public Map<String, List<LinkModel>> getLinkGroups() {
		return linkGroups;
	}

	public Map<String, String> direction() throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		Map<String, String> result = new LinkedHashMap<String, String>();
		for (Map.Entry<String, List<LinkModel>> entry : this.linkGroups.entrySet()) {
			LinkModelRelativeCaculator.caculateFocusRects(entry.getValue());
			mapper.setSerializationInclusion(Include.NON_EMPTY);

			String json = mapper.writeValueAsString(entry.getValue());
			result.put(entry.getKey(), json);

		}
		return result;
	}

	public Widget getWidget(String id) {
		for (Widget widget : widgets) {
			if (id.equals(widget.getId())) {
				return widget;
			}
		}
		return null;
	}

	public Object getUserSession() {
		return userSession;
	}

	public void setUserSession(Object userSession) {
		this.userSession = userSession;
	}

	public Object findAttribute(String name) {
		return this.pageContext.findAttribute(name);
	}

	public void setKeyHandler(String onkeypress) {
		this.keyHandler = onkeypress;
	}

	public String getKeyHandler() {
		return keyHandler;
	}

	public void setSiteCode(String siteCode) {
		this.siteCode = siteCode;
	}

	public String getPlayHandler() {
		return playHandler;
	}

	public void setPlayHandler(String playHandler) {
		this.playHandler = playHandler;
	}

	public String getBackUrl() {
		return backUrl;
	}

	public void setBackUrl(String backUrl) {
		this.backUrl = backUrl;
	}

	public LinkModel findLink(String forId) {
		for (List<LinkModel> linkGroup : this.linkGroups.values()) {
			Optional<LinkModel> result = linkGroup.stream().filter(b -> {
				return forId.equals(b.getId());
			}).findFirst();
			if (result.isPresent()) {
				return result.get();
			}
		}
		return null;

	}

	public String getScript() {
		return script.toString();
	}

	public void addScript(String content) {
		this.script.append(content);
	}

	public PageContext getPageContext() {
		return this.pageContext;
	}

	public Object getAccessContext() {
		return accessContext;
	}

	public void setAccessContext(Object accessContext) {
		this.accessContext = accessContext;
	}

	public String getDefaultImage() {

		return "/images/default_img.jpg";
	}

	public void setHistory(Object history) {
		this.history = history;

	}

	public Object getHistory() {
		return this.history;
	}

	public String getSiteCode() {
		return siteCode;
	}

	
}
